package com.vegetable.modules.service.bank.convert;

import com.alibaba.fastjson.JSONObject;
import com.vegetable.modules.entity.bank.vo.CGB;
import com.vegetable.modules.entity.bank.vo.CGBTran;
import com.vegetable.modules.entity.bank.vo.StandardizedBank;
import com.vegetable.modules.entity.bank.vo.StandardizedBankTran;

import java.util.ArrayList;
import java.util.List;

/**
 * 广发流水标准化字段输出
 */
public class CGBConverter {

	public String convert(String json) throws Exception {
		CGB cgb = JSONObject.parseObject(json, CGB.class);
		String standardizedJson = "";

		StandardizedBank standardizedBank = new StandardizedBank();
		standardizedBank.setAccountName(cgb.getAccountName());
		standardizedBank.setCardNumber(cgb.getAccountNo());
		// Assuming startDate and endDate are extracted from inquiryPeriod
		String[] dates = cgb.getInquiryPeriod().split(" 至 ");
		if (dates.length == 2) {
			standardizedBank.setStartDate(dates[0]);
			standardizedBank.setEndDate(dates[1]);
		}
		// Assuming printTime is not available in CGB, set it to null or a default value
		standardizedBank.setPrintTime(null);

		List<StandardizedBankTran> standardizedBankTrans = new ArrayList<>();
		for (CGBTran cgbTran : cgb.getCgbTrans()) {
			StandardizedBankTran standardizedBankTran = new StandardizedBankTran();
			standardizedBankTran.setTranDate(cgbTran.getTranDate().substring(0, 10));
			// Assuming tranTime is not available in CGBTran, set it to null or a default value
			standardizedBankTran.setTranTime(cgbTran.getTranDate().substring(10).trim());
			standardizedBankTran.setCurrency(cgbTran.getCurrency());
			standardizedBankTran.setAmount(cgbTran.getTranAmt().replace("+", ""));
			standardizedBankTran.setBalance(cgbTran.getAccountBalance());
			standardizedBankTran.setTranName(cgbTran.getSummary());
			standardizedBankTran.setChannel(null); // Assuming channel is not available in CGBTran
			standardizedBankTran.setCounterpartInfo(cgbTran.getCounterPartyAccountName() + "/" + cgbTran.getConterPartyAccountNo());
			standardizedBankTran.setPostscript(cgbTran.getPostscript());

			standardizedBankTrans.add(standardizedBankTran);

		}
		standardizedBank.setStandardizedBankTrans(standardizedBankTrans);

		standardizedJson = JSONObject.toJSONString(standardizedBank);
		return standardizedJson;
	}

	public static void main(String[] args) throws Exception {
		CGBConverter spdConverter = new CGBConverter();
		String spdJson = "{\"账号\":\"6214620221003646521\",\"户名\":\"苏小康\",\"起止日期\":\"2021-06-06 至 2021-09-06\",\"交易明细\":[{\"交易时间\":\"2021-09-05 23:17:58\",\"币种\":\"人民币\",\"交易金额\":\"-1446.00\",\"账户余额\":\"15856.78\",\"对方姓名\":\"财付通\",\"对方账号\":\"243300133\",\"摘要\":\"快捷支付\",\"附言\":null},{\"交易时间\":\"2021-08-07 23:07:40\",\"币种\":\"人民币\",\"交易金额\":\"-896.00\",\"账户余额\":\"17302.78\",\"对方姓名\":\"财付通-微信转账\",\"对方账号\":\"842584073990001\",\"摘要\":\"快捷支付\",\"附言\":null},{\"交易时间\":\"2021-08-07 10:56:05\",\"币种\":\"人民币\",\"交易金额\":\"-3176.00\",\"账户余额\":\"18198.78\",\"对方姓名\":\"财付通-微信转账\",\"对方账号\":\"842584073990001\",\"摘要\":\"快捷支付\",\"附言\":null},{\"交易时间\":\"2021-08-06 00:01:21\",\"币种\":\"人民币\",\"交易金额\":\"-1800.00\",\"账户余额\":\"21374.78\",\"对方姓名\":\"财付通-微信转账\",\"对方账号\":\"842584073990001\",\"摘要\":\"快捷支付\",\"附言\":null},{\"交易时间\":\"2021-08-03 11:51:05\",\"币种\":\"人民币\",\"交易金额\":\"-34500.00\",\"账户余额\":\"23174.78\",\"对方姓名\":\"苏小康\",\"对方账号\":\"6222084000001484065\",\"摘要\":\"转账支出\",\"附言\":null},{\"交易时间\":\"2021-08-01 00:15:30\",\"币种\":\"人民币\",\"交易金额\":\"-659.00\",\"账户余额\":\"57674.78\",\"对方姓名\":\"财付通\",\"对方账号\":\"243300133\",\"摘要\":\"快捷支付\",\"附言\":null},{\"交易时间\":\"2021-07-31 20:59:12\",\"币种\":\"人民币\",\"交易金额\":\"-257.00\",\"账户余额\":\"58333.78\",\"对方姓名\":\"财付通\",\"对方账号\":\"243300133\",\"摘要\":\"快捷支付\",\"附言\":null},{\"交易时间\":\"2021-07-30 09:56:21\",\"币种\":\"人民币\",\"交易金额\":\"-10000.00\",\"账户余额\":\"58590.78\",\"对方姓名\":\"苏小康\",\"对方账号\":\"6236330014121237537\",\"摘要\":\"转账支出\",\"附言\":null},{\"交易时间\":\"2021-07-29 00:45:42\",\"币种\":\"人民币\",\"交易金额\":\"-2125.00\",\"账户余额\":\"68590.78\",\"对方姓名\":\"财付通-微信转账\",\"对方账号\":\"842584073990001\",\"摘要\":\"快捷支付\",\"附言\":null},{\"交易时间\":\"2021-07-27 17:54:12\",\"币种\":\"人民币\",\"交易金额\":\"-19350.00\",\"账户余额\":\"70715.78\",\"对方姓名\":\"金程鑫\",\"对方账号\":\"6214831215079325\",\"摘要\":\"转账支出\",\"附言\":null},{\"交易时间\":\"2021-07-27 00:29:38\",\"币种\":\"人民币\",\"交易金额\":\"-1688.00\",\"账户余额\":\"90065.78\",\"对方姓名\":\"财付通\",\"对方账号\":\"243300133\",\"摘要\":\"快捷支付\",\"附言\":null},{\"交易时间\":\"2021-07-26 00:25:16\",\"币种\":\"人民币\",\"交易金额\":\"-5000.00\",\"账户余额\":\"91753.78\",\"对方姓名\":\"苏小康\",\"对方账号\":\"6236330014121237537\",\"摘要\":\"转账支出\",\"附言\":null},{\"交易时间\":\"2021-07-24 22:23:48\",\"币种\":\"人民币\",\"交易金额\":\"-5000.00\",\"账户余额\":\"96753.78\",\"对方姓名\":\"苏小康\",\"对方账号\":\"6236330014121237537\",\"摘要\":\"转账支出\",\"附言\":null},{\"交易时间\":\"2021-07-22 12:55:43\",\"币种\":\"人民币\",\"交易金额\":\"-1800.00\",\"账户余额\":\"101753.78\",\"对方姓名\":\"财付通-微信转账\",\"对方账号\":\"842584073990001\",\"摘要\":\"快捷支付\",\"附言\":null},{\"交易时间\":\"2021-07-21 14:11:38\",\"币种\":\"人民币\",\"交易金额\":\"-12900.00\",\"账户余额\":\"103553.78\",\"对方姓名\":\"金程鑫\",\"对方账号\":\"6214831215079325\",\"摘要\":\"转账支出\",\"附言\":null},{\"交易时间\":\"2021-07-14 17:34:47\",\"币种\":\"人民币\",\"交易金额\":\"-2390.00\",\"账户余额\":\"116453.78\",\"对方姓名\":\"财付通\",\"对方账号\":\"243300133\",\"摘要\":\"快捷支付\",\"附言\":null},{\"交易时间\":\"2021-07-13 22:30:06\",\"币种\":\"人民币\",\"交易金额\":\"-1581.00\",\"账户余额\":\"118843.78\",\"对方姓名\":\"财付通\",\"对方账号\":\"243300133\",\"摘要\":\"快捷支付\",\"附言\":null},{\"交易时间\":\"2021-07-11 18:37:36\",\"币种\":\"人民币\",\"交易金额\":\"-1800.00\",\"账户余额\":\"120424.78\",\"对方姓名\":\"财付通\",\"对方账号\":\"243300133\",\"摘要\":\"快捷支付\",\"附言\":null},{\"交易时间\":\"2021-07-09 10:19:54\",\"币种\":\"人民币\",\"交易金额\":\"-9864.51\",\"账户余额\":\"122224.78\",\"对方姓名\":\"财付通\",\"对方账号\":\"243300133\",\"摘要\":\"快捷支付\",\"附言\":null},{\"交易时间\":\"2021-07-05 22:48:57\",\"币种\":\"人民币\",\"交易金额\":\"-2461.00\",\"账户余额\":\"132089.29\",\"对方姓名\":\"财付通\",\"对方账号\":\"243300133\",\"摘要\":\"快捷支付\",\"附言\":null},{\"交易时间\":\"2021-07-04 21:27:52\",\"币种\":\"人民币\",\"交易金额\":\"-2070.00\",\"账户余额\":\"134550.29\",\"对方姓名\":\"财付通\",\"对方账号\":\"243300133\",\"摘要\":\"快捷支付\",\"附言\":null},{\"交易时间\":\"2021-06-29 18:43:32\",\"币种\":\"人民币\",\"交易金额\":\"-34000.00\",\"账户余额\":\"136620.29\",\"对方姓名\":\"苏小康\",\"对方账号\":\"6222084000001484065\",\"摘要\":\"转账支出\",\"附言\":null},{\"交易时间\":\"2021-06-23 22:21:37\",\"币种\":\"人民币\",\"交易金额\":\"-13000.00\",\"账户余额\":\"170620.29\",\"对方姓名\":\"苏小康\",\"对方账号\":\"6236330014121237537\",\"摘要\":\"转账支出\",\"附言\":null},{\"交易时间\":\"2021-06-22 10:38:26\",\"币种\":\"人民币\",\"交易金额\":\"-13600.00\",\"账户余额\":\"183620.29\",\"对方姓名\":\"金程鑫\",\"对方账号\":\"6214831215079325\",\"摘要\":\"转账支出\",\"附言\":null},{\"交易时间\":\"2021-06-21 19:01:34\",\"币种\":\"人民币\",\"交易金额\":\"-50000.00\",\"账户余额\":\"197220.29\",\"对方姓名\":\"苏小康\",\"对方账号\":\"6236330014121237537\",\"摘要\":\"转账支出\",\"附言\":null},{\"交易时间\":\"2021-06-21 01:12:35\",\"币种\":\"人民币\",\"交易金额\":\"+111.29\",\"账户余额\":\"247220.29\",\"对方姓名\":\"\",\"对方账号\":\"\",\"摘要\":\"结息转入\",\"附言\":null},{\"交易时间\":\"2021-06-19 10:40:10\",\"币种\":\"人民币\",\"交易金额\":\"-50000.00\",\"账户余额\":\"247109.00\",\"对方姓名\":\"苏小康\",\"对方账号\":\"6236330014121237537\",\"摘要\":\"转账支出\",\"附言\":null},{\"交易时间\":\"2021-06-15 11:35:58\",\"币种\":\"人民币\",\"交易金额\":\"-50000.00\",\"账户余额\":\"297109.00\",\"对方姓名\":\"苏小康\",\"对方账号\":\"6236330014121237537\",\"摘要\":\"转账支出\",\"附言\":null},{\"交易时间\":\"2021-06-14 11:33:58\",\"币种\":\"人民币\",\"交易金额\":\"-3000.00\",\"账户余额\":\"347109.00\",\"对方姓名\":\"财付通\",\"对方账号\":\"243300133\",\"摘要\":\"快捷支付\",\"附言\":null},{\"交易时间\":\"2021-06-12 22:55:19\",\"币种\":\"人民币\",\"交易金额\":\"-2564.00\",\"账户余额\":\"350109.00\",\"对方姓名\":\"财付通-微信转账\",\"对方账号\":\"842584073990001\",\"摘要\":\"快捷支付\",\"附言\":null},{\"交易时间\":\"2021-06-11 13:00:01\",\"币种\":\"人民币\",\"交易金额\":\"-1800.00\",\"账户余额\":\"352673.00\",\"对方姓名\":\"财付通\",\"对方账号\":\"243300133\",\"摘要\":\"快捷支付\",\"附言\":null},{\"交易时间\":\"2021-06-06 14:48:53\",\"币种\":\"人民币\",\"交易金额\":\"-2537.00\",\"账户余额\":\"354473.00\",\"对方姓名\":\"财付通\",\"对方账号\":\"243300133\",\"摘要\":\"快捷支付\",\"附言\":null}]}";
		String standardizedJson = spdConverter.convert(spdJson);
		System.out.println(standardizedJson);
	}
}
